package com.healthysys.backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.healthysys.backend.mapper.MenuMapper;
import com.healthysys.backend.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import pojo.dto.UpdateMenuDTO;
import pojo.entity.Menu;
import pojo.result.PageResult;
import pojo.result.Result;

import java.util.Collection;
import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public PageResult<Menu> getMenuListPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Menu> menus = baseMapper.selectList(null); // 使用baseMapper
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);
        return PageResult.success(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Result<Void> updateMenu(UpdateMenuDTO updateMenuDTO) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(updateMenuDTO, menu);
        boolean success = updateById(menu); // 使用Service提供的方法
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }
}